#
#INJECT JAVASCRIPT
#PMOD PLUGIN
#

from urlparse import parse_qs

name = "Inject Javascript" #Name
web = "ijavascr.html"      #Web Page File

"""javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; var _X = 500; var _Y = 500; var DI= document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+_X+"px"; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+_Y+"px";}R++}tag=setInterval('A()',5 );document.onmousemove=function(event){_X = event.clientX; _Y = event.clientY;}; void(0)"""

def setup(storage): 
    storage.javascript = ""

def edit_RESPONSE(response,storage):
    if storage.enabled:
        #response.body = response.body.replace("<html>","<html><script type='text/javascript'>"+storage.javascript+"</script>")
        response.body = response.body.replace("<head>","<html><script type='text/javascript'>"+storage.javascript+"</script>")

def do_PAGE(storage):
    out = ""
    if(storage.enabled):
        out+="<a href='ijavascr.html?disable'>Disable</a><br><br>"+storage.javascript+"<br><br>SETUP:<br><form action='ijavascr.html'><textarea name=java></textarea><br><input type=submit value='Update' name=update></form>"
    else:
        out+="<a href='ijavascr.html?enable'>Enable</a>"
    return out

def handle_HTTP_RESPONSE(response,storage):
    if response.path.find("?") >= 0: #Probably editing something...
        query = response.path[response.path.find("?")+1:]
        if query=="enable":
            storage.enabled = 1
        if query=="disable":
            storage.enabled = 0
        if "update=Update" in query: #Update
            q = parse_qs(query)
            if 'java' in q:
                storage.javascript = q['java'][0]
